package com.emm.yixun.system.service.impl.manage;

import java.text.SimpleDateFormat;
import java.util.*;

import com.emm.yixun.common.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.service.AppPushService;
import com.emm.yixun.basic.service.MessageReceiverService;
import com.emm.yixun.basic.service.NoticeService;
import com.emm.yixun.common.app.base.CommonResDTO;
import com.emm.yixun.common.model.dto.MessageReceiverDto;
import com.emm.yixun.common.model.dto.NoticeDto;
import com.emm.yixun.common.model.enums.IsSendEnum;

@Service("customerManagePushMsgManage")
public class CustomerManagePushMsgManage {

	@Autowired
	private MessageReceiverService messageReceiverService;
	@Autowired
	private AppPushService appPushService;
	@Autowired
	private NoticeService noticeService;
	
	/**
	 * 推送消息
	 * @param reqDto
	 * @return
	 */
	public CommonResDTO pushMsg() {
		CommonResDTO resDto = new CommonResDTO();
		resDto.setResult("0");
		try{
			//1、查询客户维护相关推送消息
			// NT03: 客户维护,未跟进提醒相关消息
			// NT05: 客户维护,超时未跟进提醒相关消息
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar beforeCalendar = Calendar.getInstance();
			beforeCalendar.add(Calendar.DATE, -1);
			String issueTimeBefore = DateUtils.formatDate(beforeCalendar.getTime(), DateUtils.parsePatterns[1]) + " 00:00:00";
			Calendar afterCalendar = Calendar.getInstance();
	 		int year = afterCalendar.get(Calendar.YEAR);
	 		int month = afterCalendar.get(Calendar.MONTH) + 1;
	 		int day = afterCalendar.get(Calendar.DAY_OF_MONTH);
	 		String issueTimeAfter = year + "-" + month + "-" + day + " 23:59:59";
	 		//查询客户维护、未推送且发布时间在今天的消息【未跟进NT03】
	 		MessageReceiverDto mrDto = new MessageReceiverDto();
	 		mrDto.setNoticeTempType("NT03");
	 		mrDto.setIsSend(IsSendEnum.UNSEND.getNo());
	 		mrDto.setIssueTimeBefore(sdf.parse(issueTimeBefore));
	 		mrDto.setIssueTimeAfter(sdf.parse(issueTimeAfter));
			List<MessageReceiverDto> dataList = new ArrayList<>();
	   	 	List<MessageReceiverDto> mrDtoList = messageReceiverService.selectPushMsgListByDto(mrDto);
	   	 	if(mrDtoList != null){
				dataList.addAll(mrDtoList);
	   	 	}
	   	 	
	   	 	//查询客户维护、未推送且发布时间在今天的消息【超时未跟进NT05】
	 		mrDto = new MessageReceiverDto();
	 		mrDto.setNoticeTempType("NT05");
	 		mrDto.setIsSend(IsSendEnum.UNSEND.getNo());
	 		mrDto.setIssueTimeBefore(sdf.parse(issueTimeBefore));
	 		mrDto.setIssueTimeAfter(sdf.parse(issueTimeAfter));
	   	 	List<MessageReceiverDto> mrDtoList2 = messageReceiverService.selectPushMsgListByDto(mrDto);
	   	 	if(mrDtoList2 != null){
				dataList.addAll(mrDtoList2);
	   	 	}
	   	 	
	   	 	if(dataList != null && dataList.size() > 0){
		   	 	String noticeIds = "";
	   	 		String msgRecIds = "";
	   	 		Set<String> noticeIdSet = new HashSet<String>();
	   	 		for(MessageReceiverDto msgRecDto: dataList){
	   	 			//开始推送消息
		   	 		Map<String, String> pushMap = new HashMap<String, String>();
			   	 	pushMap.put("action", "客户维护");
			   	 	pushMap.put("remindCategoryId", String.valueOf(msgRecDto.getDictionaryId()));
//			   	 	pushMap.put("pushType", msgRecDto.getNoticeTempType());
			   	 	pushMap.put("pushType", "KHWHTX");
					String context = msgRecDto.getContent();
					appPushService.pushMessage(String.valueOf(msgRecDto.getReceiverId()), context,
						msgRecDto.getTitle(), pushMap);
					
					//消息ID,Set
					noticeIdSet.add(String.valueOf(msgRecDto.getNoticeId()));
					//消息接收人ID'S
					if(StringUtils.isNotEmpty(msgRecIds)){
						msgRecIds += ",";
					}
					msgRecIds += String.valueOf(msgRecDto.getId());
	   	 		}
	   	 		//需更新消息ID'S
	   	 		if(!noticeIdSet.isEmpty()){
					for(String noticeId: noticeIdSet){
						if(StringUtils.isNotEmpty(noticeIds)){
							noticeIds += ",";
						}
						noticeIds += noticeId;
		   	 		}
	   	 		}
	   	 		//更新消息
	   	 		NoticeDto noticeDto = new NoticeDto();
	   	 		noticeDto.setIsSend(IsSendEnum.SEND.getNo());
	   	 		noticeService.updates(noticeDto, noticeIds);
	   	 		//更新接收人
	   	 		MessageReceiverDto updateMRDto = new MessageReceiverDto();
	   	 		updateMRDto.setIsSend(IsSendEnum.SEND.getNo());
	   	 		messageReceiverService.updates(updateMRDto, msgRecIds);
	   	 	}
	   	 	resDto.setResult("1");
		}catch(Exception e){
			e.printStackTrace();
			resDto.setErrorCode("00000");
			resDto.setErrorMsg("推送客户维护提醒消息失败!");
			resDto.setResult("0");
			return resDto;
		}
		return resDto;
	}
	
	
}
